﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace BS.Read.Api.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class UploadController : ControllerBase
    {
        /// <summary>
        /// 注入七牛云
        /// </summary>
        private readonly QiniuUtils _qiniuUtils;

        public UploadController(QiniuUtils qiniuUtils)
        {
            _qiniuUtils = qiniuUtils;
        }

        /// <summary>
        /// 上传文件接口
        /// </summary>
        /// <param name="file">上传的文件</param>
        /// <returns>上传结果</returns>
        [HttpPost("UploadFile")]
        public string UploadFile(IFormFile file)
        {
            // 校验文件是否为空
            if (file.Length == 0)
            {
                return "未选择文件";
            }

            // 生成文件保存名称
            string name = DateTime.Now.ToString("yyyyMMddHHmmss") + Path.GetFileName(file.FileName);

            // 使用 MemoryStream 读取文件数据
            using (var stream = new MemoryStream())
            {
                file.CopyTo(stream);
                byte[] data = stream.ToArray();

                // 调用上传方法
                string sdd = _qiniuUtils.UploadFile(data, name);
                return sdd;
            }

          
        }
    }
}
